package com.dr.java.day02;

/**
 * Created by rui on 2017/12/29.
 */
public class Operation2 {
    public static void main(String[] args) {
        System.out.println(true & false);
        System.out.println(true | false);

//        ^异或:相同为false，不同为true
        System.out.println("^");
        System.out.println(true ^ true);
        System.out.println(true ^ false);
        System.out.println(false ^ false);
        System.out.println(3 ^ 7);//0011 ^ 0111=0100=>4

//        &&只有左边为假就不执行下去了
        System.out.println("&&");
        System.out.println(writeFalse() && writeTrue());

//        ||只要左边为真就不执行下去了
        System.out.println("||");
        System.out.println(writeTrue() || writeFalse());

//        ~位运算，"取反加一"
        System.out.println(~7);

        exchageValue();
    }

    private static void exchageValue() {
        int a = 2;
        int b = 4;
        System.out.println("=========exchange two value===========");
        System.out.println("a=" + a + ";b=" + b);

//        a^b^b==a
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;

        System.out.println("a=" + a + ";b=" + b);
        System.out.println((a ^ b ^ b) == a);
    }

    private static boolean writeFalse() {
        System.out.println("write false");
        return false;
    }

    private static boolean writeTrue() {
        System.out.println("write: true");
        return true;
    }
}
